<template>
  <div class="header">

    <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
    >
      <el-form-item label="名称">
        <el-input v-model="formInline.name" placeholder="名称"/>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="formInline.status" placeholder="审核状态">
          <el-option label="" value=""/>
          <el-option label="未审核" :value="0"/>
          <el-option label="审核通过" :value="1"/>
          <el-option label="未通过" :value="-1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
            :icon="Search"
            type="primary"
            @click="queryPageColumnList"
        >
          搜索
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button
            :icon="Delete"
            type="warning"
            @click="clearFormLine"
        >
          清空
        </el-button>
      </el-form-item>
    </el-form>
  </div>

  <div class="main">
    <el-card :body-style="{ display: 'flex', padding: 0 }" v-for="item in columnList" :key="item.id">
      <div class="detail">
        <img :src="item.coverImage" width="100" height="100">
        <div style="display: flex;align-items: center;flex-direction: column">
          <el-tag type="primary" style="font-size: 14px;" effect="dark" size="large">
            {{ item.name }}
          </el-tag>
          <el-tag type="success" style="font-size: 14px;" effect="dark" size="large">
            访问量：{{ item.accessNumber }}
          </el-tag>
          <el-tag type="warning" style="font-size: 14px;" effect="dark" size="large">
            评论量：{{ item.commentNumber }}
          </el-tag>
        </div>
        <el-select
            v-model="item.status"
            style="margin-left: 30px;width: 125px"
            placeholder="请审核"
            @change="updateStatus(item.status,item.id)"
            >
          <el-option
              v-for="status in statusList"
              :key="status.id"
              :label="status.label"
              :value="status.id"
          />
        </el-select>
      </div>
    </el-card>
  </div>

  <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageTotal"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px"
  />
</template>

<script setup>
import {ref} from 'vue'
import {Search, Delete} from '@element-plus/icons-vue'
import request from "../utils/request";
import {ElMessage} from "element-plus";


const pageNum = ref(1)
const pageSize = ref(5)
const pageTotal = ref(10)
const formInline = ref({
  name: '',
  status: '',
  pageParam: {
    pageNum: pageNum.value,
    pageSize: pageSize.value
  }
})
const originForm = {
  name: '',
  status: 0,
  pageParam: {
    pageNum: 1,
    pageSize: pageSize.value
  }
}
const columnList = ref([])
const statusList = [
  {id:0,label:'未审核'},
  {id:1,label:'审核通过'},
  {id:-1,label:'审核未通过'}
]

//改变状态
const updateStatus = (status,id) => {
  const columnStatusParma = {
    id,status
  }
  request.put('/contents/posts/special-column/manage/status',columnStatusParma).then(res => {
    if (res.code === 1){
      ElMessage.success(res.data)
      queryPageColumnList()
    }else {
      ElMessage.error(res.message)
    }
  })
}

//改变页码
const handleSizeChange = (val) => {
  formInline.value.pageParam.pageSize = val
  queryPageColumnList()
}
//改变当前页
const handleCurrentChange = (val) => {
  formInline.value.pageParam.pageNum = val
  queryPageColumnList()
}
//清空表单
const clearFormLine = () => {
  formInline.value = {...originForm}
}
//分页查询
const queryPageColumnList = () => {
  request.post('/contents/posts/special-column/manage/page',formInline.value).then(res => {
    if (res.code === 1){
      columnList.value = res.data.records
      pageNum.value = res.data.current
      pageSize.value = res.data.size
      pageTotal.value = res.data.total
    }
  })
}
queryPageColumnList()

</script>


<style lang="less" scoped>
.main{
  .el-card{
    .detail{
      .el-tag{
        margin-bottom: 5px;
      }
      display: flex;
      align-items: center;
    }
    width: 400px;
    margin: 10px;
    padding: 10px;
  }
  display: flex;
  align-items: center;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.demo-form-inline {
  margin-left: 25px;
}

.header {
  width: 100%;
  margin-top: 20px;
  margin-bottom: 10px;
  display: flex;
}
</style>
